﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using AlertSystem.PluggableModules;
using StorageManagement;
using AlertSystem.Business;
using System.Data;

namespace AlertSystem.UI
{
    public class ReaderThreadManage : WorkingBaseTask
    {
        protected override void LoopWorking()
        {
            List<Warning_Message> warningList = Warning_Message.GetAll(AppSetting.BatchSize);
            if (warningList != null && warningList.Count > 0)
            {
                List<Warning_Log> logs = new List<Warning_Log>();
                DataTable dtGuid = new DataTable();
                dtGuid.Columns.Add("TypeName", typeof(Guid));
                foreach (var msg in warningList)
                {
                    bool find = false;
                    foreach (var method in Warning_Method.GetInstance())
                    {
                        if (
                            Warning_System_Type_Account.Any(wsta => wsta.WarningType == msg.WarningType && wsta.System_Id == msg.WarningSystem
                                && Warning_Account.Any(wa => wa.Account_Id == wsta.Account_Id
                                    && Warning_Account_Method.Any(wam => wam.Account_Id == wa.Account_Id && wam.Method_Id == method.Method_Id)))

                        )
                        {
                            find = true;
                            if (DictionaryStorage<int, Warning_Message>.IsFull(method.Method_Id))
                            {
                                Warning_Message fail_msg = DictionaryStorage<int, Warning_Message>.DequeueElementData(method.Method_Id);
                                if (fail_msg != null)
                                {
                                    logs.Add(new Warning_Log()
                                    {
                                        LogMessage = string.Format("OVERFLOW_QUEUE: [GUID={0}, Method_ID={2}]", fail_msg.Message_Guid, method.Method_Id),
                                        LogSource = "ReaderThreadManage",
                                        LogTime = DateTime.Now,
                                        LogTrace = "ReaderThreadManage.LoopWorking"
                                    });
                                }
                            }
                            DictionaryStorage<int, Warning_Message>.EnqueueElementData(method.Method_Id, msg);
                        }
                    }
                    if (!find)
                    {
                        logs.Add(new Warning_Log()
                        {
                            LogMessage = string.Format("SEND_METHOD_NULL: [GUID={0}]", msg.Message_Guid),
                            LogSource = "ReaderThreadManage",
                            LogTime = DateTime.Now,
                            LogTrace = "ReaderThreadManage.LoopWorking"
                        });
                        dtGuid.Rows.Add(msg.Message_Guid);
                    }
                }
                if (logs.Count > 0)
                {
                    //Insert vào database
                    DataAccess.BulkInsert<Warning_Log>(logs, "Warning_Log");
                    logs.Clear();
                }
                if (dtGuid.Rows.Count > 0)
                {
                    Warning_Message_Statistic.UpdateFailStatus(dtGuid);
                    dtGuid.Dispose();
                }
            }
        }
    }
}
